package com.ormlite.activity.tool;

import java.util.Arrays;
import java.util.List;

import android.app.Activity;
import android.util.SparseIntArray;
import android.widget.ArrayAdapter;
import android.widget.SpinnerAdapter;

import com.j256.ormlite.dao.RuntimeExceptionDao;
import com.ormlite.db.OrmLiteHelper;
import com.ormlite.pojo.Event;
import com.ormlite.pojo.Movie;

public class DatabaseUtils {

	private DatabaseUtils() {
		// no instance needed
	}

	public static List<Movie> findMoviesByTitle(String title) {
		RuntimeExceptionDao<Movie, Integer> movieDao = OrmLiteHelper.getMovieRuntimeDao();
		return movieDao.queryForEq("title", title);
	}

	public static List<Event> findEventsByName(String name) {
		RuntimeExceptionDao<Event, Integer> eventDao = OrmLiteHelper.getEventRuntimeDao();
		return eventDao.queryForEq("name", name);
	}

	public static Movie findMovieById(int id) {
		RuntimeExceptionDao<Movie, Integer> movieDao = OrmLiteHelper.getMovieRuntimeDao();
		return movieDao.queryForId(id);
	}

	public static Event findEventById(int id) {
		RuntimeExceptionDao<Event, Integer> eventDao = OrmLiteHelper.getEventRuntimeDao();
		return eventDao.queryForId(id);
	}

	public static int updateMovie(Movie movie) {
		RuntimeExceptionDao<Movie, Integer> movieDao = OrmLiteHelper.getMovieRuntimeDao();
		return movieDao.update(movie);
	}

	public static int updateEvent(Event event) {
		RuntimeExceptionDao<Event, Integer> eventDao = OrmLiteHelper.getEventRuntimeDao();
		return eventDao.update(event);
	}

	public static int removeMovie(Movie movie) {
		RuntimeExceptionDao<Event, Integer> eventDao = OrmLiteHelper.getEventRuntimeDao();
		eventDao.delete(eventDao.queryForEq("movie_id", movie));
		RuntimeExceptionDao<Movie, Integer> movieDao = OrmLiteHelper.getMovieRuntimeDao();
		return movieDao.delete(movie);
	}

	public static int removeEvent(Event event) {
		RuntimeExceptionDao<Event, Integer> eventDao = OrmLiteHelper.getEventRuntimeDao();
		return eventDao.delete(event);
	}

	public static int updateMovieById(int id, Movie change) {
		Movie movie = DatabaseUtils.findMovieById(id);
		movie.setFullTitle(change.getTitle());
		movie.setDirector(change.getDirector());
		movie.setYear(change.getYear());
		movie.setRate(change.getRate());
		return DatabaseUtils.updateMovie(movie);
	}

	public static int updateEventById(int id, Event change) {
		Event event = DatabaseUtils.findEventById(id);
		event.setName(change.getName());
		event.setMovie(change.getMovie());
		event.setTime(change.getTime());
		event.setComment(change.getComment());
		return DatabaseUtils.updateEvent(event);
	}

	public static List<Movie> getAllMovies() {
		RuntimeExceptionDao<Movie, Integer> movieRuntimeDao = OrmLiteHelper.getMovieRuntimeDao();
		return movieRuntimeDao.queryForAll();
	}

	public static List<Event> getAllEvents() {
		RuntimeExceptionDao<Event, Integer> eventRuntimeDao = OrmLiteHelper.getEventRuntimeDao();
		return eventRuntimeDao.queryForAll();
	}

	public static int saveMovie(Movie movie) {
		RuntimeExceptionDao<Movie, Integer> eventRuntimeDao = OrmLiteHelper.getMovieRuntimeDao();
		return eventRuntimeDao.create(movie);
	}

	public static int saveEvent(Event event) {
		RuntimeExceptionDao<Event, Integer> eventRuntimeDao = OrmLiteHelper.getEventRuntimeDao();
		return eventRuntimeDao.create(event);
	}

	public static SpinnerAdapter createAdapter(Activity activity, SparseIntArray positionToId) {

		List<Movie> movies = DatabaseUtils.getAllMovies();
		String[] titles = Utils.extractDataFromMovies(false, movies, positionToId);
		ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(activity, android.R.layout.simple_spinner_item,
				Arrays.asList(titles));
		dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

		return dataAdapter;
	}
}
